﻿@model NetSurveillance.WebApp.DataServices.Models.RemoteServer
@{
    Layout = "~/Views/Shared/_SystemConfigLayout.cshtml";
    ViewBag.Title = "远程控制";
    ViewBag.CurrentMiddleMenu = 6;
    ViewBag.CurrentMainMenu = 5;
}
@section css{
    <style type="text/css">
        fieldset
        {
            width: 80%;
            margin: 10px auto;
        }
    </style>
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    <fieldset class="inline-fieldset">
        <legend>@ViewBag.Title</legend>
        <table id="tbRemoteSvr">
            <tr>
                <td>开启远程控制功能:</td>
                <td>
                    <label>
                        @Html.RadioButtonFor(m=>m.StatusToken, 1,
                                             new { id = "rd_isOpen_yes" })
                        @Resources.Resource.Yes
                    </label>
                    <label>
                         @Html.RadioButtonFor(m=>m.StatusToken, 0,
                                             new { id = "rd_isOpen_no" })
                        @Resources.Resource.No
                    </label>
                </td>
            </tr>
            <tr>
                <td>远程IP:</td>
                <td>
                    @Html.TextBoxFor(m => m.Ip)
                    @Html.ValidationMessageFor(m => m.Ip)
                </td>
            </tr>
            <tr>
                <td>远程端口:</td>
                <td>
                    @Html.TextBoxFor(m => m.Port)
                    @Html.ValidationMessageFor(m => m.Port)
                </td>
            </tr>
            <tr>
                <td>远程用户名:</td>
                <td>
                    @Html.TextBoxFor(m => m.Username)
                    @Html.ValidationMessageFor(m => m.Username)
                </td>
            </tr>
            <tr>
                <td>远程密码:</td>
                <td>
                    @Html.TextBoxFor(m => m.Pass)
                    @Html.ValidationMessageFor(m => m.Pass)
                </td>
            </tr>
        </table>
        @Html.HiddenFor(m => m.Id)
    </fieldset>
    <div class="buttonsContain">
        <ul class="config_bottom_btndivs">
            <li>
                <input type="submit" id="btnSaveQuery" value="@Resources.Resource.Button_Apply"/>
            </li>
            <li>
                <input type="reset" id="btnResetChanges" value="@Resources.Resource.Button_ResetChanges"/>
            </li>
        </ul>
    </div>
}
@section javascript
{
    <script type="text/javascript">
        $(function() {
            $('#btnSaveQuery').click(checkSetRmSvr);
            $('#rd_isOpen_yes,#rd_isOpen_no').change(toggleRmtSvr);
            toggleRmtSvr();
        });

        function checkSetRmSvr() {
            $.showLoading('正在设置，请等待');
            sendChk();
            return false;
        }

        function sendChk() {
            var rmSvrObj = $('form').serializeObject();
            $.ajax({
                url: '@Url.Action("CheckRmSvr")',
                cache: false,
                type: 'post',
                dataType: 'json',
                data: rmSvrObj,
                success: function (result) {
                    if (result.success && !result.expandos) {
                        setTimeout(sendChk, 3000);
                        return;
                    }
                    if (result.success && result.expandos && result.expandos.isValid)
                        $('form').trigger('submit');
                    if (!result.success) {
                        alert(result.message);
                        $.hideLoading();
                    }
                }
            });
        }

        function toggleRmtSvr() {
            var $rdYes = $('#rd_isOpen_yes'),
                $rdNo = $('#rd_isOpen_no');
            if ($rdNo.is(':checked')) {
                $rdNo.closest('tr').siblings().find(':input').attr('disabled', 'disabled');
            }
            if ($rdYes.is(':checked')) {
                $rdYes.closest('tr').siblings().find(':input').removeAttr('disabled');
            }
        }
    </script>
}
